package net.tinyos.tools;

import java.io.*;
import net.tinyos.packet.*;
import net.tinyos.util.*;
import net.tinyos.message.*;
import java.util.Arrays;

public class Listen {
	public static float arr2float (byte[] arr, int start) {
		int i = 0;
		int len = 4;
		int cnt = 0;
		byte[] tmp = new byte[len];
		for (i = start; i < (start + len); i++) {
			tmp[cnt] = arr[i];
			cnt++;
		}
		int accum = 0;
		i = 0;
		for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) {
			accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
			i++;
		}
		return Float.intBitsToFloat(accum);
	}
	public static int arr2int (byte[] arr, int start) {
		int low = arr[start] & 0xff;
		int high = arr[start+1] & 0xff;
		return (int)( high << 8 | low );
	}
    public static void main(String args[]) throws IOException {
	if (args.length > 0) {
	    System.err.println("usage: java net.tinyos.tools.Listen");
	    System.exit(2);
	}
	
	PacketSource reader = BuildSource.makePacketSource();
	if (reader == null) {
	    System.err.println("Invalid packet source (check your MOTECOM environment variable)");
	    System.exit(2);
	}

	try {
	  reader.open(PrintStreamMessenger.err);
	  for (;;) {
	    byte[] packet = reader.readPacket();
          
		byte[] posY = {packet[17],packet[18],packet[15],packet[16]};
	      byte[] posX = {packet[13],packet[14],packet[11],packet[12]};
		byte[] id = {packet[5],packet[6]};          

	    System.out.println("wysyla " + arr2int(id,0) + ":  " + "X: " + arr2float(posX,0) + " Y: "+ arr2float(posY,0));
	    //Dump.printPacket(System.out, packet);
	    System.out.println();
	  }
	}
	catch (IOException e) {
	    System.err.println("Error on " + reader.getName() + ": " + e);
	}
    }
}

